﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.BO;

namespace PPAP.Web.Part
{
    public partial class PPARTermination : PageBase
    {
        PPARBO bo = new PPARBO();
        PPARBO ppbo = new PPARBO();
        LogBO lbo = new LogBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                DataSet ds = null;
                if (CurrentUser.UnitId == "0")
                    ds = ppbo.GetPlantListForPlantID(CurrentUser.UnitName);
                else
                    ds = ppbo.GetPlantListForSupplierid(CurrentUser.UnitId == "99" ? CurrentUser.Id : null);
                ddlPlant.Items.Add(new ListItem("All", "All"));
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        ddlPlant.Items.Add(new ListItem(dr["plantname"].ToString(), dr["id"].ToString()));
                    }
                }
                else
                {
                    ddlPlant.Items.Add(new ListItem("Not added plant information", "0"));
                }
                if (CurrentUser.UnitId == "0" || CurrentUser.UnitId == "99")  //供应商点操作，跳转到SupplierPPARWeb页面
                {
                    dgList.Columns[8].Visible = false;
                    dgList.Columns[15].Visible = false;

                    ddlStatus.Items.Add(new ListItem("All", "All"));
                    ddlStatus.Items.Add(new ListItem("N/A", "0"));
                    ddlStatus.Items.Add(new ListItem("Created", "1"));
                    ddlStatus.Items.Add(new ListItem("Submitted", "2"));
                    ddlStatus.Items.Add(new ListItem("Ready for sampling", "3"));
                    ddlStatus.Items.Add(new ListItem("Not ready for sampling", "4"));
                    ddlStatus.Items.Add(new ListItem("Decided", "7"));
                }
                else //后台用户点操作，跳转到CreatPPAPWeb页面
                {
                    dgList.Columns[9].Visible = false;
                    dgList.Columns[14].Visible = false;

                    ddlStatus.Items.Add(new ListItem("All", "All"));
                    ddlStatus.Items.Add(new ListItem("N/A", "0"));
                    ddlStatus.Items.Add(new ListItem("Created", "1"));
                    ddlStatus.Items.Add(new ListItem("Submitted", "2"));
                    ddlStatus.Items.Add(new ListItem("Ready for sampling", "3"));
                    ddlStatus.Items.Add(new ListItem("Not ready for sampling", "4"));
                    ddlStatus.Items.Add(new ListItem("Circulating", "5"));
                    ddlStatus.Items.Add(new ListItem("Released", "6"));
                    ddlStatus.Items.Add(new ListItem("Decided", "7"));
                    ddlStatus.Items.Add(new ListItem("Termination", "9"));
                }
                btnSubmit_Click(null, null);
            }
            if (Request.QueryString["rid"] != null) //终止
            {
                Delete(Request.QueryString["rid"].ToString());
            }

            if (Request.QueryString["aid"] != null) //激活
            {
                Delete2(Request.QueryString["aid"].ToString());
            }
        }

        private void Delete(string id)
        {
            int num = ppbo.UpdateTermination(id);
            if (num > 0)
            {
                lbo.WriteLog(CurrentUser.Id, "Termination PPAR", id);
                AlertMessage("Termination succeed!");
                btnSubmit_Click(null, null);
            }
        }

        private void Delete2(string id)
        {
            if (ppbo.GetMoreVersion(id).Rows.Count > 0)
            {
                AlertMessage("A higher version PPAR has created,you can not re-active the lower version.");
            }
            else
            {
                int num = ppbo.UpdateTermination2(id);
                if (num > 0)
                {
                    lbo.WriteLog(CurrentUser.Id, "Activation PPAR", id);
                    AlertMessage("Activation succeed!");
                    btnSubmit_Click(null, null);
                }
            }
        }

        //状态显示

        public string ShowAuditStatus(string Status)
        {
            string result = "";
            result = "<span style='color:red'>已提交</span>";

            switch (Status.ToLower())
            {
                case "0":
                    result = "<span style='color:blue'></span>";
                    break;
                case "1":
                    result = "<span style='color:red'>Created</span>";
                    break;
                case "2":
                    result = "<span style='color:red'>Submitted</span>";
                    break;
                case "3":
                    result = "<span style='color:red'>Ready for sampling</span>";
                    break;
                case "4":
                    result = "<span style='color:red'>Not ready for sampling</span>";
                    break;
                case "5":
                    result = "<span style='color:red'>Circulating</span>";
                    break;
                case "6":
                    result = "<span style='color:blue'>Released</span>";
                    break;
                case "7":
                    result = "<span style='color:blue'>Decided</span>";
                    break;
                case "9":
                    result = "<span style='color:red'>Termination</span>";
                    break;
                default:
                    result = "<span style='color:red'></span>";
                    break;
            }
            return result;
        }

        public string ShowAuditStatus2(string Status)  //供应商看到的状态与后台用户看到的状态不同
        {
            string result = "";
            result = "<span style='color:red'>已提交</span>";

            switch (Status.ToLower())
            {
                case "0":
                    result = "<span style='color:blue'></span>";
                    break;
                case "1":
                    result = "<span style='color:red'>Created</span>";
                    break;
                case "2":
                    result = "<span style='color:red'>Submitted</span>";
                    break;
                case "3":
                    result = "<span style='color:red'>Ready for sampling</span>";
                    break;
                case "4":
                    result = "<span style='color:red'>Not ready for sampling</span>";
                    break;
                case "5":
                    result = "<span style='color:red'>Ready for sampling</span>";
                    break;
                case "6":
                    result = "<span style='color:red'>Ready for sampling</span>";
                    break;
                case "7":
                    result = "<span style='color:blue'>Decided</span>";
                    break;

                default:
                    result = "<span style='color:red'></span>";
                    break;
            }
            return result;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string sqlwhere = "";

            if (txtPartno.Text.Trim() != "")
                sqlwhere += "   and lower(bdntpartno) like lower('%" + txtPartno.Text.Trim() + "%') ";
            if (ddlPlant.SelectedValue != "All")
                sqlwhere += "   and lower(p.plantname) like lower('%" + ddlPlant.SelectedItem.Text + "%') ";
            if (txtPartName.Text.Trim() != "")
                sqlwhere += " and lower(bdntpartname) like lower('%" + txtPartName.Text.Trim() + "%') ";
            if (ddlStatus.SelectedValue != "All")
            {
                if ((CurrentUser.UnitId == "0" || CurrentUser.UnitId == "99") && ddlStatus.SelectedValue == "3") //供应商
                    sqlwhere += " and (a.status=3 or a.status=5 or a.status=6) ";
                else
                    sqlwhere += " and a.status=" + ddlStatus.SelectedValue;

            }
            if (tbVersion.Text.Trim() != "")
                sqlwhere += " and lower(bdntrevisionno) like lower('%" + tbVersion.Text.Trim() + "%') ";
            if (DatePicker1.Text.Trim() != "")
                sqlwhere += " and trunc(a.ppardate)>=to_date( '" + DatePicker1.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (DatePicker2.Text.Trim() != "")
                sqlwhere += " and trunc(a.ppardate)<=to_date( '" + DatePicker2.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtColor.Text.Trim() != "")
                sqlwhere += " and lower(r.color) like lower('%" + txtColor.Text.Trim() + "%') ";
            if (txtResult.Text.Trim() != "")
                sqlwhere += " and lower(FINALRESULT)=lower('" + txtResult.Text.Trim() + "') and a.status=7 ";
            //if (txtSuppliername.Text.Trim() != "")
            //    sqlwhere += " and lower(s.suppliername) like lower('%" + txtSuppliername.Text.Trim() + "%') ";
            //if (txtSuppliercode.Text.Trim() != "")
            //    sqlwhere += " and lower(s.suppliercode) like lower('%" + txtSuppliercode.Text.Trim() + "%') ";
            //if (txtPPARNO.Text.Trim() != "")
            //    sqlwhere += " and lower('PPAR-'||a.pparno||'.'||PPARVERSION) like lower('%" + txtPPARNO.Text.Trim() + "%') ";

            if (CurrentUser.UnitId == "99")
                sqlwhere += " and p.supplieradmin='" + CurrentUser.Id + "'";
            if (CurrentUser.UnitId == "0")
                sqlwhere += " and r.plantid=" + CurrentUser.UnitName;
            DataSet ds = bo.GetListForWhere3(sqlwhere);// bo.GetListForWhere(txtPartno.Text, ddlPlant.SelectedItem.Text, cbAll.Checked, CurrentUser.UnitId == "0" ? CurrentUser.UnitName : null);
            AspNetPager2.RecordCount = ds.Tables[0].Rows.Count;
            dgList.DataSource = GetPageTable(ds.Tables[0], AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            dgList.DataBind();
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            btnSubmit_Click(null, null);
        }


    }
}